﻿using MCMesServer.DBFunctions.DBFuncs;
using MCMesServer.DBFunctions.ReceiveFuncHandler;
using MCMesServer.DBFunctions.TransformFuncs;
using MCMesServer.Model;
using MCMesServer.NetConnectFunc;
using MCMesServer.PublicLogInfo;
using MesDAL.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.ServerFunctions.StateFunc
{
    public class StateMsgServerHandlerFunc
    {
        public static void HandleReceiveMsg(ServerModel model, IntPtr connId)
        {
            try
            {
                switch (model.Operation)
                {
                    case Operations.UploadState:
                        UploadState(model, connId);
                        break;
                    case Operations.DownloadState:
                        DownloadState(model, connId);
                        break;
                    case Operations.SelectProgram:
                        SelectProgram(model, connId);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonConnectFunc.SendServerError(connId, model.MachineNum, model.Operation, model.Step, ServerResCode.ERR_UnExpectOfState, ex.ToString());
                return;
            }
        }

        #region 方法
        /// <summary>
        /// 上报设备状态
        /// </summary>
        /// <param name="model"></param>
        /// <param name="connId"></param>
        /// <param name="orignNum"></param>
        private async static void UploadState(ServerModel model, IntPtr connId)
        {
            try
            {
                #region 识别传输状态
                string machineStateStr = model.Message;

                //当前数据库中设备状态记录
                MachineInfoDTO machineInfo = await MachineInfoDBFunc.SelectMachineInfoByMachineNum(model.MachineNum);
                if (!machineInfo.machine_state.Equals(machineStateStr))//上传状态与数据库内记录的不一致
                {
                    bool isRecordSuccess = await StateChangeHandler.RecordMachineStateLog(machineInfo.machine_state, machineStateStr, machineInfo.machine_num);
                    bool isEditState = false;
                    if (isRecordSuccess)//成功记录设备状态变更记录
                    {
                        machineInfo.machine_state = machineStateStr;
                        isEditState = await StateChangeHandler.ChangeMachineState(machineInfo);
                        if (isEditState)
                        {
                            string ModelResult = TransformModelFunc<MachineInfoDTO>.TransformModel2StringFunc(machineInfo);
                            CommonConnectFunc.SendInterClientModelFunc(connId, machineInfo.machine_num, model.Operation, model.Step, ServerResCode.OPE_UploadStateSuccess, ModelResult);
                        }
                        else
                        {
                            CommonConnectFunc.SendInterClientModelFunc(connId, machineInfo.machine_num, model.Operation, model.Step, ServerResCode.ERR_UploadStateFail, "");
                        }
                    }
                    else
                    {
                        //记录设备状态失败
                        CommonConnectFunc.SendInterClientModelFunc(connId, machineInfo.machine_num, model.Operation, model.Step, ServerResCode.ERR_UploadStateFail, "");
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage("上报设备状态", $"设备号：{model.MachineNum} DTO：{model.Message} ##ErrorMessage：{ex.ToString()}");
                CommonConnectFunc.SendServerError(connId, model.MachineNum, model.Operation, model.Step, ServerResCode.ERR_UnExpectOfState, ex.ToString());
                return;
            }
        }

        /// <summary>
        /// 查询设备状态
        /// </summary>
        /// <param name="model"></param>
        /// <param name="connId"></param>
        private async static void DownloadState(ServerModel model, IntPtr connId)
        {
            try
            {
                MachineInfoDTO machineInfo = await MachineInfoDBFunc.SelectMachineInfoByMachineNum(model.MachineNum);

                string result = TransformModelFunc<MachineInfoDTO>.TransformModel2StringFunc(machineInfo);
                CommonConnectFunc.SendInterClientModelFunc(connId, machineInfo.machine_num, model.Operation, model.Step, ServerResCode.OPE_DownloadStateSuccess, result);
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage("获取设备在服务器上状态", $"设备号：{model.MachineNum} DTO：{model.Message} ##ErrorMessage：{ex.ToString()}");
                CommonConnectFunc.SendServerError(connId, model.MachineNum, model.Operation, model.Step, ServerResCode.ERR_UnExpectOfState, ex.ToString());
                return;
            }
        }

        /// <summary>
        /// 选择设备程序
        /// </summary>
        /// <param name="model"></param>
        /// <param name="connId"></param>
        private async static void SelectProgram(ServerModel model, IntPtr connId)
        {
            try
            {

            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage("选择设备程序", $"设备号：{model.MachineNum} DTO：{model.Message} ##ErrorMessage：{ex.ToString()}");
                CommonConnectFunc.SendServerError(connId, model.MachineNum, model.Operation, model.Step, ServerResCode.ERR_UnExpectOfState, ex.ToString());
            }
        }
        #endregion
    }
}
